package com.xc.ksxt.controller;

import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.Helper;
import com.xc.db.tables.Jie;
import com.xc.ksxt.entity.ChapterEntity;
import com.xc.ksxt.entity.LibQuestionEntity;
import com.xc.ksxt.entity.JieEntity;
import com.xc.ksxt.entity.PianEntity;
import com.xc.ksxt.service.ChapterService;
import com.xc.ksxt.service.JieService;
import com.xc.ksxt.service.PianService;
import com.xc.ksxt.vo.JieVO;
import com.xc.sys.service.SysUserService;
import com.xc.sys.vo.SysUserVO;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/jie")
@CrossOrigin
public class JieController {

    @Autowired
    private JieService jieService;
    private Jie JIETB = Jie.JIE.as("JIETB");
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getPageList"})
    public LayuiTableResponse<JieVO> getPageList(HttpServletRequest request) {
        LayuiTableResponse<JieVO> ltr = new LayuiTableResponse<>();
        Condition cond = JIETB.DEL_FLAG.notEqual(-1);
        String name = request.getParameter("name");
        String pianId = request.getParameter("pianId");
        String chapterId = request.getParameter("chapterId");
        String keywords = request.getParameter("keywords");
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer page = Integer.valueOf(request.getParameter("page"));
        if (StringUtils.isNotBlank(name)) {
            cond = cond.and(JIETB.NAME.like("%"+name+"%"));
        }
        if (StringUtils.isNotBlank(keywords)) {
            cond = cond.and(JIETB.NAME.like("%"+keywords+"%"));
        }
        if (StringUtils.isNotBlank(pianId)) {
            cond = cond.and(JIETB.PIAN_ID.eq(Long.valueOf(pianId)));
        }
        if (StringUtils.isNotBlank(chapterId)&&!chapterId.equals("null")) {
            cond = cond.and(JIETB.CHAPTER_ID.eq(Long.valueOf(chapterId)));
        }
        Long count = jieService.count(cond);
        ltr.setCount(count);
        PageInfo<JieEntity> pageInfo = new PageInfo<>(limit, page);
        Result<Record> list = jieService.select(cond, pageInfo,JIETB.ORDERNO.asc(),JIETB.NAME.asc(),JIETB.CREATE_TIME.desc());
        List<JieVO> jieEntityList = list.into(JieVO.class);
        for (JieVO jieEntity : jieEntityList){
            SysUserVO user = SysUserService.getFrameCaches(jieEntity.getCreateUserId());
            jieEntity.setCreateUserName(user==null?"":user.getName());
            if(jieEntity.getPianId()!=null){
                PianEntity pian = PianService.getFromCaches(jieEntity.getPianId());
                jieEntity.setPianName(pian==null?"":pian.getName());
            }
            if(jieEntity.getChapterId()!=null){
                ChapterEntity chapter = ChapterService.getFromCaches(jieEntity.getChapterId());
                jieEntity.setChapterName(chapter==null?"":chapter.getName());
            }
        }
        ltr.setData(jieEntityList);
        return ltr;
    }

    @RequestMapping({"/getById"})
    public JieEntity getById(Integer id) {
        JieEntity entity = jieService.getEntityById(id);
        return entity;
    }

    @RequestMapping({"/save"})
    public ResponseEntity<JieEntity> save(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
        ResponseEntity<JieEntity> res = new ResponseEntity<>();
        JieEntity jieEntity = Helper.map2Bean(new JieEntity(), params);
        jieService.save(jieEntity);
        return res;
    }
    @RequestMapping({"/updateOrderno"})
    public ResponseEntity<LibQuestionEntity> updateOrderno(@RequestBody HashMap<String,String> params) {
        Integer id = Integer.valueOf(params.get("id"));
        Integer orderno = Integer.valueOf(params.get("orderno"));
        ResponseEntity<LibQuestionEntity> res = new ResponseEntity<>();
        JieEntity lq = jieService.getEntityById(id);
        lq.setOrderno(orderno);
        jieService.update(lq);
        return res;
    }
    @RequestMapping({"/delete"})
    public LayuiTableResponse<JieEntity> delete(@RequestBody Map<String,String> params) {
        String ids = params.get("ids");
        Integer[] idarr = Helper.str2integerArr(ids);
        LayuiTableResponse<JieEntity> ltr = new LayuiTableResponse<>();
        for (Integer id : idarr) {
            JieEntity m = jieService.getEntityById(id);
            m.setDelFlag(-1);
            jieService.update(m);
        }
        return ltr;
    }

}
